package com.manish.tabdemo;

import java.util.ArrayList;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.manish.tabdemo.R;
import com.manish.tabdemo.R.drawable;
import com.manish.tabdemo.R.id;
import com.manish.tabdemo.R.layout;
import com.manish.tabdemo.R.menu;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class GoogleMapsActivity extends FragmentActivity implements
		ActionBar.OnNavigationListener {

	/** An array of strings to populate dropdown list */
	String[] actions = new String[] { "Dove Mangiare", "Dove Dormire",
			"Bar e Tabacchi", "Turismo" };

	// action bar
	private ActionBar actionBar;

	// Title navigation Spinner data
	private ArrayList<SpinnerNavItem> navSpinner;

	// Navigation adapter
	private TitleNavigationAdapter adapter;

	// coordinate su cui centrare la mappa
	private final LatLng CENTER_POINT = new LatLng(42.0453571, 14.7288686);

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mappe);

		actionBar = getActionBar();

		// Hide the action bar title
		actionBar.setDisplayShowTitleEnabled(true);
		
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);

		// Enabling Spinner dropdown navigation
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

		// Spinner title navigation data
		navSpinner = new ArrayList<SpinnerNavItem>();
		navSpinner.add(new SpinnerNavItem(actions[0],
				R.drawable.ic_action_search));
		navSpinner.add(new SpinnerNavItem(actions[1],
				R.drawable.ic_action_refresh));
		navSpinner.add(new SpinnerNavItem(actions[2],
				R.drawable.ic_action_search));
		navSpinner.add(new SpinnerNavItem(actions[3],
				R.drawable.ic_action_refresh));

		// title drop down adapter
		adapter = new TitleNavigationAdapter(getApplicationContext(),
				navSpinner);

		// assigning the spinner navigation
		actionBar.setListNavigationCallbacks(adapter, this);

		/*******************************************************************/

		// ottieni il controllo del fragment su cui caricare la mappa
		GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();
		// centra la mappa su CENTER_POINT, con zoom 5
		map.moveCamera(CameraUpdateFactory.newLatLngZoom(CENTER_POINT, 14));

		/*******************************************************************/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Actionbar navigation item select listener
	 * */
	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		// Action to be taken after selecting a spinner item
		// if (itemPosition == 1){
		// Intent intent = new Intent(MapActivity.this, CittaActivity.class);
		// startActivity(intent);
		// }

		return false;
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	onBackPressed();
        return super.onOptionsItemSelected(item);       
    }
}
